﻿package ui.control
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import ui.GuiWnd;
	import ui.control.controlStruct.TagTreeNode;

	public class GuiTree extends GuiWnd
	{
		public var m_bdOpen:BitmapData;
		public var m_bdClose:BitmapData;

		public var m_vecNode:Vector.<TagTreeNode >  = new Vector.<TagTreeNode >   ;

		public var m_objNodeList:Object = new Object();

		public function GuiTree()
		{

		}
		
		override public function init(xml:XML):void
		{
			
		}
		
		/**
		 * 设置打开状态图片 
		 * @param img
		 * 
		 */		
		public function set openImg(img:BitmapData):void
		{
			m_bdOpen = img;
		}
		
		/**
		 * 设置关闭状态的图片 
		 * @param img
		 * 
		 */		
		public function set closeImg(img:BitmapData):void
		{
			m_bdClose = img;
		}

		
		/**
		 * 添加元素 
		 * @param str  显示文本
		 * @param strName 唯一标识名称
		 * @param strFather 父
		 * @param img 图标
		 * @param data 附带值
		 * @param index 下标
		 * 
		 */		
		public function addItem(str:String, strName:String, strFather:String="", img:Sprite=null, data:Object=null, index:int=-1):void
		{

			if (str=="" || strName == "")
			{
				return;
			}

			if (getNodeByName(strName))
			{
				return;
			}

			var node:TagTreeNode = new TagTreeNode();
			node.htmlText = str;
			node.strName = strName;
			node.data = data;

			var father:TagTreeNode = getNodeByName(strFather);
			if (father)
			{
				node.fatherNode = father;
				father.addItem(node, index);
			}
			else
			{
				m_vecNode.push(node);

				addChild(node);
			}
			
			if (m_bdOpen && m_bdClose)
			{
				node.setStateIco(m_bdOpen, m_bdClose);
			}

			if (img)
			{
				node.img = img;
			}

			m_objNodeList[strName] = node;

			node.addEventListener(MouseEvent.CLICK, onEvent);
			updataPos();
		}

		public function onEvent(e:MouseEvent):void
		{
			var item:TagTreeNode = e.target as TagTreeNode;
			if (! item)
			{
				item = e.currentTarget as TagTreeNode;
			}
			if (! item)
			{
				return;
			}

			if (item.vecNode.length)
			{
				item.isOpen = ! item.isOpen;
				updataPos();
			}
			else
			{
				trace(item.strName);
			}
			e.stopPropagation();

		}

		/**
		 * 删除 
		 * @param strName
		 * 
		 */		
		public function deleteItem(strName:String):void
		{
			var node:TagTreeNode = getNodeByName(strName);
			if (! node)
			{
				return;
			}

			var father:TagTreeNode = node.fatherNode;
			if (father)
			{
				father.deleteItem(node);
			}
			else
			{
				var index:int = m_vecNode.indexOf(node);
				if (index != -1)
				{
					m_vecNode.splice(index, 1);
				}

				delete m_objNodeList[strName];
			}
			updataPos();
		}

		/**
		 * 设置元素显示文本 
		 * @param str
		 * @param strName
		 * 
		 */		
		public function setItemText(str:String, strName:String):void
		{
			if (str || strName == "")
			{
				return;
			}

			var node:TagTreeNode = getNodeByName(strName);
			if (node)
			{
				node.htmlText = str;
			}
		}
		
		/**
		 * 获取元素显示的文本 
		 * @param strName
		 * @return 
		 * 
		 */		
		public function getItemText(strName:String):String
		{
			var str:String = "";
			
			if (strName || strName == "")
			{
				return str;
			}
			
			var node:TagTreeNode = getNodeByName(strName);
			if (node)
			{
				str = node.htmlText;
			}
			
			return str
		}
		
		/**
		 * 获取元素的值 
		 * @param strName
		 * @return 
		 * 
		 */		
		public function getItemData(strName:String):Object
		{
			if (strName || strName == "")
			{
				return null;
			}
			var node:TagTreeNode = getNodeByName(strName);
			if (node)
			{
				return  node.data;
			}
			
			return null
			
		}

		/**
		 * 设置元素的图标 
		 * @param img
		 * @param strName
		 * 
		 */		
		public function setItemImg(img:Sprite, strName:String):void
		{
			if (img || strName == "")
			{
				return;
			}

			var node:TagTreeNode = getNodeByName(strName);
			if (node)
			{
				node.img = img;
			}
		}

		/**
		 * 打开或关闭元素 
		 * @param strName
		 * @param bOpen
		 * 
		 */		
		public function openItem(strName:String, bOpen:Boolean):void
		{
			var item:TagTreeNode = getNodeByName(strName);
			if (! item)
			{
				return;
			}

			item.isOpen = bOpen;

			updataPos();

		}

		/**
		 * 更新坐标 
		 * 
		 */		
		private function updataPos():void
		{
			var pos:int = 0;
			for (var i:int=0; i<m_vecNode.length; i++)
			{
				var node:TagTreeNode = m_vecNode[i];
				node.y = pos;
				pos +=  node.height + 2;
			}
		}

		private function getNodeByName(strName:String):TagTreeNode
		{
			return m_objNodeList[strName];
		}


		//public function ShowItem(strName:String, bShow:Boolean)
		//{
		//var item:tagTreeNode = getNodeByName(strName);
		//if(!item) return;
		//
		//item.visible = bShow;
		//
		//if(item.FatherNode)
		//item.FatherNode.UpdataItemPos();
		//
		//updataPos();
		//}

	}

}